﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class CompanyPartnerEditControl : YuanlinUserControl,
        IReplyConverter<CompanyPartnerReply, GetOperation>
    {
        protected ValuedTextBox titleValuedTextBox;
        protected ValuedDropDownList categoryDropDownList;
        protected ValuedTextBox linkUrlValuedTextBox;

        protected IButton SaveButton;
        protected IButton BackButton;
        private CompanyPartner companyPartner;
        private YuanlinUser user = HttpContext.Current.User as YuanlinUser;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetCompanyPartnerRequest(this));
        }
        protected override void OnLoad(EventArgs e)
        {
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        private void Bind()
        {
            if (companyPartner != null)
            {
                this.titleValuedTextBox.Value = companyPartner.Title.Value;
                this.linkUrlValuedTextBox.Value = companyPartner.LinkUrl.Value;

                CompanyPartnerCategoryListRequest request = new CompanyPartnerCategoryListRequest();
                request.UserId = user.EntityId.Value;
                request.PageIndex = 0;
                request.PageSize = 1000;
                EntityList categories = BusinessManager.GetCompanyPartnerCategoryList(RequestHelper.BuildGetCompanyPartnerCategoryListRequest(request));

                categoryDropDownList.DataSource = categories;
                categoryDropDownList.DataValueField = "EntityId";
                categoryDropDownList.DataTextField = "Title";
                categoryDropDownList.DataBind();
                categoryDropDownList.Value = companyPartner.CategoryId.Value.ToString();
            }   
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            if (companyPartner != null)
            {
                companyPartner.Title.Value = CensorWordManager.CensorWord(this.titleValuedTextBox.Value);
                companyPartner.LinkUrl.Value = CensorWordManager.CensorWord(this.linkUrlValuedTextBox.Value);
                companyPartner.CategoryId.Value = int.Parse(categoryDropDownList.Value);

                if (BusinessManager.UpdateCompanyPartner(companyPartner) == -2)
                {
                    Globals.ShowMessage(this.Page, "修改失败", "此信息已经存在！");
                    return;
                }
            }
            PageRedirect();
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetCompanyPartnerListUrl());
        }
        #region IReplyConverter<CompanyPartnerReply,GetOperation> Members

        public void ConvertReply(CompanyPartnerReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                companyPartner = reply.Entity as CompanyPartner;

                if (!Page.IsPostBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}